﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Facebook.Web.Mvc;
using AzureFacebook.Domain;
using AzureFacebook.Data;
using AzureFacebook.Web.Models;

namespace AzureFacebook.Web.Controllers
{
    public class HomeController : Controller
    {
        readonly ICommentService _commentService;

        public HomeController(ICommentService commentService)
        {
            _commentService = commentService;
            AutoMapper.Mapper.CreateMap<CommentModel, Comment>();
        }

        [FacebookAuthorization(IsFbml=false)]
        public ActionResult Index()
        {

            var fbapi = this.GetApi();
            var uid = fbapi.Users.GetLoggedInUser();
            Facebook.Schema.user loggedInUser = fbapi.Users.GetInfo(uid);


            ViewData["FirstName"] = loggedInUser.first_name;
            ViewData["FacebookId"] = uid.ToString();

            var model = new CommentModel();
            model.FacebookId = uid.ToString();

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(CommentModel model)
        {
            if (ModelState.IsValid)
            {
                var c = new Comment();
                AutoMapper.Mapper.Map(model, c);

                _commentService.Add(c);
            }

            return View("Index");
        }

        public ActionResult Documentation()
        {
            return View();
        }


    }
}
